home *** CD-ROM | disk | FTP | other *** search
/ C++ für Kids / C++ for kids.iso / SETUP / US / CBUILDER / DATA.Z / FOOTMAIN.CPP < prev    next >
C/C++ Source or Header  |  1997-02-13  |  13KB  |  450 lines

  1. //----------------------------------------------------------------------------
  2. //Borland C++Builder
  3. //Copyright (c) 1987, 1997 Borland International Inc. All Rights Reserved.
  4. //----------------------------------------------------------------------------
  5. //---------------------------------------------------------------------------
  6. #include <vcl.h>
  7. #pragma hdrstop
  8.                                               
  9. #include <vcl/mmsystem.hpp>
  10. #include "footmain.h"
  11. #include "instr.h"
  12. //---------------------------------------------------------------------------
  13. #pragma resource "*.dfm"
  14. TForm1 *Form1;
  15. char numbuf[10];
  16. //---------------------------------------------------------------------------
  17. __fastcall TForm1::TForm1(TComponent* Owner)
  18.   : TForm(Owner)
  19. {
  20. }
  21. //---------------------------------------------------------------------------
  22. void __fastcall TForm1::MoveForward(TObject *Sender)
  23. {
  24.     if ((OnOffSwitch->Position == 2) || (LastSack != NULL))
  25.         return;
  26.  
  27.     if (!running)
  28.         TogglePlay(true);
  29.  
  30.     if (Runningback->Left < TACKLEROW3 - 2)
  31.     {
  32.         if (field[rbx+1][rby].player != NULL)
  33.         {
  34.             Sacked(field[rbx+1][rby].player);
  35.             return;
  36.         }
  37.         field[rbx][rby].player = NULL;
  38.         field[rbx][rby].rb = false;
  39.         Runningback->Left += 22;
  40.         rbx++;
  41.     }
  42.     else
  43.     {
  44.         if (field[0][rby].player != NULL)
  45.         {
  46.             Sacked(field[0][rby].player);
  47.             return;
  48.         }
  49.         field[rbx][rby].player = NULL;
  50.         field[rbx][rby].rb = false;
  51.         Runningback->Left = RBLEFT;
  52.         rbx = 0;
  53.     }
  54.  
  55.     YardsToGo--;
  56.     FieldPosition++;
  57.     if (FieldPosition == 100)
  58.     {
  59.         sndPlaySound("td.wav", SND_SYNC);
  60.         running = false;
  61.         ShowField(false);
  62.         Home+=7;
  63.         HomeDisplay->Caption = itoa(Home, numbuf, 10);
  64.         FieldPosition=80;  // Starting position for visitor
  65.         Down = 1;
  66.         YardsToGo = 10;
  67.         Computer->Visible = true;
  68.         return;
  69.     }
  70.     field[rbx][rby].player = Runningback;
  71.     field[rbx][rby].rb = true;
  72. }
  73. //---------------------------------------------------------------------
  74. void __fastcall TForm1::MoveUp(TObject *Sender)
  75. {
  76.     if ((OnOffSwitch->Position == 2) || (LastSack != NULL))
  77.         return;
  78.  
  79.     if (!running)
  80.         TogglePlay(true);
  81.  
  82.     if (Runningback->Top == RBTOP)
  83.         return; // already as up as it gets
  84.  
  85.     if (field[rbx][rby-1].player != NULL)
  86.     {
  87.         Sacked(field[rbx][rby-1].player);
  88.         return;
  89.     }
  90.  
  91.     if (Runningback->Top == RBBOTTOM)
  92.         Runningback->Top = RBMIDDLE;
  93.     else if (Runningback->Top == RBMIDDLE)
  94.         Runningback->Top = RBTOP;
  95.  
  96.     field[rbx][rby].player = NULL;
  97.     field[rbx][rby].rb = false;
  98.     rby--;
  99.     field[rbx][rby].player = Runningback;
  100.     field[rbx][rby].rb = true;
  101. }
  102. //---------------------------------------------------------------------
  103. void __fastcall TForm1::MoveDown(TObject *Sender)
  104. {
  105.     if ((OnOffSwitch->Position == 2) || (LastSack != NULL))
  106.         return;
  107.  
  108.     if (!running)
  109.         TogglePlay(true);
  110.  
  111.     if (Runningback->Top == RBBOTTOM)
  112.         return; // already as down as it gets
  113.  
  114.     if (field[rbx][rby+1].player != NULL)
  115.     {
  116.         Sacked(field[rbx][rby+1].player);
  117.         return;
  118.     }
  119.  
  120.     if (Runningback->Top == RBTOP)
  121.         Runningback->Top = RBMIDDLE;
  122.     else if (Runningback->Top == RBMIDDLE)
  123.         Runningback->Top = RBBOTTOM;
  124.  
  125.     field[rbx][rby].player = NULL;
  126.     field[rbx][rby].rb = false;
  127.     rby++;
  128.     field[rbx][rby].player = Runningback;
  129.     field[rbx][rby].rb = true;
  130. }
  131. //---------------------------------------------------------------------
  132. void __fastcall TForm1::Switched(TObject *Sender)
  133. {
  134.     switch (OnOffSwitch->Position)
  135.     {
  136.             case 1:    {
  137.                     Timer->Interval = 250;
  138.                     HomeDisplay->Visible = true;
  139.                     VisitorDisplay->Visible = true;
  140.                     TimeDisplay->Visible = true;
  141.                     ResetGame(NULL);
  142.                     break;
  143.                 }
  144.                 case 2:    {
  145.                     ShowField(false);
  146.                     HomeDisplay->Visible = false;
  147.                     VisitorDisplay->Visible = false;
  148.                     TimeDisplay->Visible = false;
  149.                     break;
  150.                 }
  151.                 case 3:    {
  152.                     Timer->Interval = 100;
  153.                     HomeDisplay->Visible = true;
  154.                     VisitorDisplay->Visible = true;
  155.                     TimeDisplay->Visible = true;
  156.                     ResetGame(NULL);
  157.                     break;
  158.                 }
  159.         }
  160. }
  161. //---------------------------------------------------------------------
  162. void __fastcall TForm1::ShowField(bool visible)
  163. {
  164.     Runningback->Visible = visible;
  165.     Tackler1->Visible = visible;
  166.     Tackler2->Visible = visible;
  167.     Tackler3->Visible = visible;
  168.     Tackler4->Visible = visible;
  169.     Tackler5->Visible = visible;
  170.  
  171. }
  172. //---------------------------------------------------------------------
  173. void __fastcall TForm1::TimerTimer(TObject *Sender)
  174. {
  175.     int x, y, newx, newy, direction;
  176.  
  177.     newy = y = random(3);
  178.     newx = x = random(10);
  179.     direction = random(2); // 0 is for x, 1 is for y
  180.  
  181.     if (!running)
  182.     {
  183.         if (LastSack != NULL)
  184.             LastSack->Visible = !LastSack->Visible;
  185.         return;
  186.     }
  187.  
  188.     if (field[x][y].rb == true)
  189.         return; // can't move the runningback!
  190.  
  191.     if (field[x][y].player == NULL)
  192.         return; // no tacker at this spot
  193.  
  194.     if (field[x][y].player->Left <= RBLEFT)
  195.         return; // already at the endzone
  196.  
  197.     if (direction == 0)  // we're moving horizontal
  198.     {
  199.         if (x < rbx)
  200.             newx = x +1;
  201.         else if (x > rbx)
  202.             newx = x -1;
  203.         else return;
  204.         //  we're already horizontally lined up with rb
  205.     }
  206.     else if (direction == 1)  // we're moving vertical
  207.     {
  208.         if (y < rby)
  209.             newy = y +1;
  210.         else if (y > rby)
  211.             newy = y -1;
  212.         else return;
  213.         //  we're already vertically lined up with rb
  214.     }
  215.  
  216.     if (field[newx][newy].rb == true) // got him!
  217.     {
  218.         Sacked(field[x][y].player);
  219.         return;
  220.     }
  221.  
  222.     if (field[newx][newy].player == NULL) // not blocked
  223.     {
  224.         field[x][y].player->Left -= (22 * (x - newx));
  225.         field[x][y].player->Top -= (16 * (y - newy));
  226.         field[newx][newy].player = field[x][y].player;
  227.         field[x][y].player = NULL;
  228.         return;
  229.     }
  230. }
  231. //---------------------------------------------------------------------
  232. void __fastcall TForm1::Sacked(TLabel *player)
  233. {
  234.     sndPlaySound("whistle.wav", SND_SYNC);
  235.     running = false;
  236.     LastSack = player;
  237.     if (YardsToGo <=0)
  238.     {
  239.         Down = 1;
  240.         YardsToGo = 10;
  241.     }
  242.     else
  243.     {
  244.         Down++;
  245.         if (Down > 4)
  246.         {
  247.             sndPlaySound("whistle.wav", SND_SYNC);
  248.             Down = 1;  // First down for visitor
  249.             YardsToGo = 10;
  250.             Computer->Visible = true;
  251.         }
  252.     }
  253. }
  254. //---------------------------------------------------------------------
  255. void __fastcall TForm1::ResetGame(TObject *Sender)
  256. {
  257.     YardsToGo=10;
  258.     Down=1;
  259.     FieldPosition=20;
  260.     Home=0;
  261.     Visitor=0;
  262.     Quarter = 1;
  263.     TimeLeft=15;
  264.     TimeDisplay->Caption = FloatToStrF(TimeLeft, ffGeneral, 4, 4);
  265.     HomeDisplay->Caption = itoa(Home, numbuf, 10);
  266.     VisitorDisplay->Caption = itoa(Visitor, numbuf, 10);
  267.     randomize;
  268.     ResetField();
  269. }
  270. //---------------------------------------------------------------------
  271. void __fastcall TForm1::ResetField()
  272. {
  273.     TogglePlay(false);
  274.     LastSack = NULL;
  275.  
  276.     // empty the field
  277.     for (int y=0; y<3; y++)
  278.         for (int x=0; x<10; x++)
  279.         {
  280.             field[x][y].player = NULL;
  281.             field[x][y].rb = false;
  282.         }
  283.  
  284.     // initial locations of players
  285.     field[0][1].player=Runningback;
  286.     field[0][1].rb=true;
  287.     rbx = 0;
  288.     rby = 1;
  289.